//
//  TestWYURLSession.m
//  NSURLSessionTaskStudy
//
//  Created by WuShiHai on 14/11/2016.
//  Copyright © 2016 WY. All rights reserved.
//

#import "TestWYURLSession.h"

@implementation TestWYURLSession

+ (void)monitorMaxTheadNumberPerHost{
    
    NSInteger requestNumber = 100;
    for (NSInteger i = 0; i < requestNumber; i++) {
        NSMutableURLRequest *reqeust = [NSMutableURLRequest requestWithURL:[[NSURL alloc] initWithString:@"http://127.0.0.1:8080/ManagementService/fileUpload"]];
        reqeust.HTTPMethod = @"POST";
        reqeust.HTTPBody = [[@(i) stringValue] dataUsingEncoding:NSUTF8StringEncoding];
        NSURLSessionDataTask *task = [[WYURLSession sharedSession] dataTaskWithRequest:reqeust completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"%@",[NSString stringWithFormat:@"%@:%@",[[NSString alloc] initWithData:reqeust.HTTPBody encoding:NSUTF8StringEncoding],error?@"failed":@"success"]);
            if (error) {
                
            }
        }];
        [task resume];
    }
    
}

+ (void)monitorMaxTheadNumberPerHostAfterSpecifyNumber{
    
    NSInteger requestNumber = 100;
    
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.HTTPMaximumConnectionsPerHost = 10;
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
    
    for (NSInteger i = 0; i < requestNumber; i++) {
        NSMutableURLRequest *reqeust = [NSMutableURLRequest requestWithURL:[[NSURL alloc] initWithString:@"http://127.0.0.1:8080/ManagementService/fileUpload"]];
        reqeust.HTTPMethod = @"POST";
        reqeust.HTTPBody = [[@(i) stringValue] dataUsingEncoding:NSUTF8StringEncoding];
    

        NSURLSessionDataTask *task = [session dataTaskWithRequest:reqeust completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"%@",[NSString stringWithFormat:@"%@:%@",[[NSString alloc] initWithData:reqeust.HTTPBody encoding:NSUTF8StringEncoding],error?@"failed":@"success"]);
            if (error) {
                
            }
        }];
        [task resume];
    }
    
}

+ (void)monitorMaxTheadNumberPerHostWithMutleSession{
    NSInteger sessionNumber = 100;
    NSInteger requestNumber = 1;
    for (NSInteger i = 0; i < sessionNumber; i++) {
        
        NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
        
        for (NSInteger j = 0 ; j < requestNumber; j++) {
            NSMutableURLRequest *reqeust = [NSMutableURLRequest requestWithURL:[[NSURL alloc] initWithString:@"http://127.0.0.1:8080/ManagementService/fileUpload"]];
            reqeust.HTTPMethod = @"POST";
            reqeust.HTTPBody = [[@(i * requestNumber + j ) stringValue] dataUsingEncoding:NSUTF8StringEncoding];
            NSURLSessionDataTask *task = [session dataTaskWithRequest:reqeust completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSLog(@"%@",[NSString stringWithFormat:@"%@:%@",[[NSString alloc] initWithData:reqeust.HTTPBody encoding:NSUTF8StringEncoding],error?@"failed":@"success"]);
                if (error) {
                    
                }
            }];
            [task resume];
        }
    }
}

@end
